package com.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;


public class StudentUseSave extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String Lab_ID ;
    private String Student_Username ;
    private String Equipment_Name ;
	public String getLab_ID() {
		return Lab_ID;
	}

	public void setLab_ID(String labID) {
		Lab_ID = labID;
	}

	public String getStudent_Username() {
		return Student_Username;
	}

	public void setStudent_Username(String studentUsername) {
		Student_Username = studentUsername;
	}
	
	public String getEquipment_Name() {
		return Equipment_Name;
	}

	public void setEquipment_Name(String equipment_name) {
		Equipment_Name = equipment_name;
	}
	
	private int Uselog_ID;
	private int Uselog_StudentID;
	private int Uselog_EquipmentID;
	private String Uselog_Date;

	public int getUselog_ID() {
		return Uselog_ID;
	}

	public void setUselog_ID(int uselog_ID) {
		Uselog_ID = uselog_ID;
	}

	public int getUselog_StudentID() {
		return Uselog_StudentID;
	}

	public void setUselog_StudentID(int uselog_StudentID) {
		Uselog_StudentID = uselog_StudentID;
	}

	public int getUselog_EquipmentID() {
		return Uselog_EquipmentID;
	}

	public void setUselog_EquipmentID(int uselog_EquipmentID) {
		Uselog_EquipmentID = uselog_EquipmentID;
	}

	public String getUselog_Date() {
		return Uselog_Date;
	}

	public void setUselog_Date(String uselog_Date) {
		Uselog_Date = uselog_Date;
	}


	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码，用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language='javascript'>alert('请重新登录！');window.location='Login.jsp';</script>");
			out.flush();out.close();return null;
		}
		
		//查询用户名是否存在
		List<StudentBean> list=new StudentDao().GetAllList("Student_Username='"+Student_Username+"'", "");
		if(list.size()<1)
		{
			out.print("<script language='javascript'>alert('学号不存在！');history.back(-1);</script>");
			out.flush();out.close();return null;
		}
		
		//添加使用记录
		StudentBean stbean=new StudentBean();
		stbean=new StudentDao().GetUnBean(Student_Username);
		EquipmentBean eqbean=new EquipmentBean();
		eqbean=new EquipmentDao().GetEqBean(Equipment_Name);
		
		UselogBean cnbean=new UselogBean();
		cnbean.setUselog_StudentID(stbean.getStudent_ID());
		cnbean.setUselog_EquipmentID(eqbean.getEquipment_ID());
		cnbean.setUselog_Date(this.getNowdate());

		new UselogDao().Add(cnbean);
		    
		//跳转
		out.print("<script language='javascript'>alert('操作成功！');window.location='StudentUse.action';</script>");
		out.flush();out.close();return null;
		
	}
	
	//获取当前日期
	public String getNowdate(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(System.currentTimeMillis());   
	}
	
	//判断是否空值
	private boolean isInvalid(String value) {
		return (value == null || value.length() == 0);
	}
	
	//测试
	public static void main(String[] args) {
		System.out.println();
	}
	
}
